//
//  FMModelTool.m
//  面向对象的数据库操作
//
//  Created by appbanana on 2017/3/18.
//  Copyright © 2017年 appbanana. All rights reserved.
//

#import "FMModelTool.h"
#import <objc/message.h>

@implementation FMModelTool

+ (NSString *)tableNameWithClass:(Class )cls{
    return NSStringFromClass(cls);
}

+ (NSString *)tmpTableNameWithClass:(Class )cls{
    return [NSStringFromClass(cls) stringByAppendingString:@"_tmp"];
}


+ (NSDictionary *)modelIvarNameAndIvarType:(Class)cls{
    
    NSMutableDictionary *modelDic = [NSMutableDictionary dictionary];
    unsigned int outCount;
    NSArray *ignoreArr;
    if ([cls respondsToSelector:@selector(ignoreColumnNames)]) {
        ignoreArr = [cls ignoreColumnNames];
    }
   Ivar *ivars = class_copyIvarList(cls, &outCount);
    for (int i = 0; i < outCount; i++) {
        const char *cName = ivar_getName(ivars[i]);
        NSString *name = [NSString stringWithUTF8String:cName];
        if ([name hasPrefix:@"_"]) {
            name = [name substringFromIndex:1];
        }
        if ([ignoreArr containsObject:name]) {
            continue ;
        }
        const char *cType = ivar_getTypeEncoding(ivars[i]);
        NSString *type = [NSString stringWithUTF8String:cType];
        type = [type stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@\""]];
        [modelDic setValue:type forKey:name];
    }
    return modelDic;
}


+ (NSDictionary *)modelIvarNameAndSqliteType:(Class)cls{
    NSDictionary *modleDic = [self modelIvarNameAndIvarType:cls];
    NSDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [modleDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [dic setValue:[self ivarTypeToSqliteTypeDic][obj] forKey:key];
    }];
    return dic;
}

+ (NSString *)modelIvarNameAndSqliteTypeStr:(Class)cls{
    
    //    create table if not exists Student3 (stu_id integer, name text, age integer, primary key(stu_id));
    NSDictionary *dic = [self modelIvarNameAndSqliteType:cls];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *string = [NSString stringWithFormat:@"%@ %@", key, obj];
        [array addObject:string];
    }];
    return [array componentsJoinedByString:@","];
}

+ (NSArray *)modelIvarSortedName:(Class)cls{
    NSDictionary *dic = [self modelIvarNameAndIvarType:cls];
    NSArray *ivarArr = dic.allKeys;
    [ivarArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    return ivarArr;
}


+ (NSDictionary *)ivarTypeToSqliteTypeDic {
    return @{
             @"d": @"real", // double
             @"f": @"real", // float
             
             @"i": @"integer",  // int
             @"q": @"integer", // long
             @"Q": @"integer", // long long
             @"B": @"integer", // bool
             
             @"NSData": @"blob",
             @"NSDictionary": @"text",
             @"NSMutableDictionary": @"text",
             @"NSArray": @"text",
             @"NSMutableArray": @"text",
             
             @"NSString": @"text"
             };
    
}

@end
